package Testclass.ajaxTest;

import com.alibaba.fastjson.JSONObject;
import Dao.jdbcTools;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/ajax7")
public class ajax7 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");//响应用户请求时，告知用户服务器的编码
        String id= req.getParameter("id");
        System.out.println("id = " + id);
        JSONObject jsonObject = JSONObject.parseObject(id.toString());
        Integer id1 = Integer.valueOf( jsonObject.getString("id"));
        int delete = 0;
        try {
            delete = jdbcTools.delete("delete from students where id = ?", id1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("delete = " + delete);
        PrintWriter writer = resp.getWriter();
        writer.write("success");
    }
}
